package com.scau.clothes.controller;

import com.scau.clothes.pojo.OrderClothes;
import com.scau.clothes.pojo.User;
import com.scau.clothes.service.CartService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
public class CartController {
    @Autowired
    CartService cartService;
    @RequestMapping("addClothesToCart")
    public ModelAndView addClothesToCartByClothesId(HttpServletRequest request, int clothesId, int num){
        User user = (User) request.getAttribute("user");
        int userId = user.getId();
        cartService.addClothesToCartByUserId(userId,clothesId,num);
        List<OrderClothes> ocList = cartService.getOCByUserIdInRedis(userId);
        ModelAndView mv = new ModelAndView();
        mv.addObject("ocList",ocList);
        mv.setViewName("input");
        return mv;
    }
    @RequestMapping("getCarts")
    public ModelAndView getOCByUserId(HttpServletRequest request){
        User user = (User) request.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        int userId = user.getId();
        List<OrderClothes> ocList = cartService.getOCByUserIdInRedis(userId);
        mv.addObject("ocList",ocList);
        mv.setViewName("cart");
        return mv;
    }
}
